package com.gilight.base.controller;


import com.gilight.base.admin.order.AdminOrderService;
import com.gilight.base.admin.product.AdminProductService;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.base.exception.ServiceException;
import com.gilight.base.dto.goods.AdminSpuDTO;
import com.gilight.base.dto.order.OrderDTO;
import com.gilight.base.dto.order.OrderStatisticsByDayDTO;
import com.gilight.base.dto.order.OrderStatisticsDTO;
import com.gilight.base.model.Page;
import com.gilight.base.model.order.OrderDO;
import com.gilight.base.service.SysUserZzjgService;
import com.gilight.base.util.MapUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author liken
 * 商品管理控制类
 */
@RestController
@RequestMapping("/web/admin-order")
public class AdminOrderController {

    @Autowired
    private AdminOrderService adminOrderService;
    @Autowired
    private SysUserZzjgService sysUserZzjgService;
    /**
     *
     * @param page    订单页码
     * @param limit   页码长度
     * @param status  订单状态
     * @param orderNo 订单号
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "订单页码")
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultEntity<Page<OrderDO>> list(
            Integer page,
            Integer limit,
            Integer status,
            String orderNo,
            Long adminId,
            Long start,
            Long end,
            Long zzjgId) throws ServiceException{
        ResultEntity resultEntity = ResultEntity.success();
        List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
        if(zzjgId == null){
             zzjgId = 1L;
            if (zzjgsUser != null && zzjgsUser.size() > 0) {
                zzjgId =  MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
            }
        }

        resultEntity.setData(adminOrderService.list(page,limit,status,orderNo,adminId,zzjgId,start,end));
        return resultEntity;

    }

    /**
     * 退款
     * @param type        0.拒绝退款 1.同意退款
     * @param sum         退款金额
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "退款")
    @RequestMapping(value = "/refund")
    public ResultEntity refund(
            String orderNo,
            Integer type,
            Integer sum,
            Long adminId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.refund(orderNo,type,sum,adminId,-1L));
        return resultEntity;


    }

    /**
     * 发货
     * @param orderNo   订单号
     * @param shipCode  承运商Code
     * @param shipNo    运单号
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "发货")
    @RequestMapping(value = "/ship")
    public ResultEntity ship(
            String orderNo,
            String shipCode,
            String shipNo,
            Long adminId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();

        List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
        Long zzjgId = -1L;
        resultEntity.setData(adminOrderService.ship(orderNo,shipCode,shipNo,adminId,zzjgId));
        return resultEntity;
    }

    /**详情
     *
     * @param orderNo 订单Id
     * @param adminId
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/detail")
    public ResultEntity<OrderDTO> detail(
            Long orderNo,
            Long adminId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.detail(orderNo,adminId));
        return resultEntity;
    }

    /**
     * 根据传入时间和订单状态查询订单信息
     * @param gmtStart  查询开始时间
     * @param gmtEnd    查询结束时间
     * @param status    订单状态
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "根据传入时间和订单状态查询订单信息")
    @RequestMapping(value = "/query-to-excel")
    public ResultEntity<List<OrderDTO>> queryToExcel(
            Long gmtStart,
            Long gmtEnd,
            Integer status,
            Long adminId, Long zzjgId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.queryToExcel(gmtStart,gmtEnd,status,adminId,zzjgId));
        return resultEntity;
    }

    /**
     * 编辑管理员备注
     * @param orderId  订单Id
     * @param level    备注等级
     * @param mono     备注
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "编辑管理员备注")
    @RequestMapping(value = "/edit-admin-mono")
    public ResultEntity editAdminMono(
            Long orderId,
            Integer level,
            String mono,
            Long adminId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.editAdminMono(orderId,level,mono,adminId));
        return resultEntity;

    }

    /**
     * 订单统计
     * @param gmtStart 查询开始时间
     * @param gmtEnd   查询结束时间
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "订单统计")
    @RequestMapping(value = "/statistics")
    public ResultEntity<List<OrderStatisticsDTO>> statistics(
            Long gmtStart,
            Long gmtEnd,
            Long adminId, Long zzjgId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.statistics(gmtStart,gmtEnd,adminId,zzjgId));
        return resultEntity;
    }

    /**
     * 订单统计
     * @param gmtStart 查询开始时间
     * @param gmtEnd   查询结束时间
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "订单统计")
    @RequestMapping(value = "/statistics/by-day")
    public ResultEntity<List<OrderStatisticsByDayDTO>> statisticsByDay(
            Long gmtStart,
            Long gmtEnd,
            Long adminId,
            Long zzjgId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.statisticsByDay(gmtStart,gmtEnd,adminId,zzjgId));
        return resultEntity;
    }
    /**
     * 订单统计
     * @param gmtStart 查询开始时间
     * @param gmtEnd   查询结束时间
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "下载微信对账单")
    @RequestMapping(value = "/statistics/we-chart/bill")
    public ResultEntity<List<OrderStatisticsByDayDTO>> weChartBill(
            Long gmtStart,
            Long gmtEnd,
            Long adminId,
            Long zzjgId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.statisticsByDayWx(gmtStart,gmtEnd,adminId,zzjgId));
        return resultEntity;
    }
}
